<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .wrap {
        width: 100%;
        height: 100%;
      }
      input {
        display: block;
        width: 300px;
        height: 30px;
        margin: 10px auto;
      }
      #box {
        width: 300px;
        height: 100px;
        border: 2px solid #ccc;
        margin: 0 auto;
        margin-top: 100px;
        font-size: 50px;
      }
    </style>
  </head>
  <body>
    <div class=".wrap">
      <input type="text" value id="I" />
      <input type="text" value id="O" />
      <input type="button" value="提交" id="btn" />
      <div id="box"></div>
    </div>
  </body>
  <script>
    // 1.密码强度
    btn.onclick = function () {
      O.value = "";
      let str = I.value;
      let number = false,
        UpCase = false,
        LowerCase = false,
        other = false;
      for (let i in str) {
        if (str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57) {
          number = true;
        } else if (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) {
          UpCase = true;
        } else if (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122) {
          LowerCase = true;
        } else {
          other = true;
        }
      }
      if (other) {
        O.value = "输入错误，仅由大小写字母、数字组成";
      } else {
        if (number + UpCase + LowerCase == 3) {
          O.value = "强";
        } else if (number + UpCase + LowerCase == 2) {
          O.value = "中";
        } else if (number + UpCase + LowerCase == 1) {
          O.value = "弱";
        }
      }
    };
    // 2.数字字母混合验证码
    function Verification_Code(n) {
      let str = "",
        res = "";
      for (let i = 0; i < n; i++) {
        if (!parseInt(Math.random() * 3)) {
          // 取0时随机数字
          res += str.concat(String(parseInt(Math.random() * 10)));
        } else if (parseInt(Math.random() * 3) === 1) {
          // 取1时随机大写字母
          res += str.concat(
            String.fromCharCode(parseInt(Math.random() * 26) + 65)
          );
          // str += String.fromCharCode(parseInt(Math.random() * 26) + 65);  //这种方法也行
        } else {
          // 取2时随机小写字母
          res += str.concat(
            String.fromCharCode(parseInt(Math.random() * 26) + 97)
          );
        }
      }
      return res;
    }
    box.innerText = Verification_Code(4);
    // 3.已知字符串“a,a,b,c,c,d”，统计每个字符出现的次数
    // 结果显示 a 2、b 1、c 2、d1，去掉重复的字符，使结果显示 abcd
    function str_unique(str) {
      let newStr = [],
        res = [],
        count = [];
      newStr = str.split(",");
      //   newStr.sort();
      res = newStr.slice(0); //复制数组
      //数组去重
      for (let i = 0; i < newStr.length - 1; i++) {
        if (newStr[i] == newStr[i + 1]) {
          newStr.splice(i + 1, 1);
          i--;
        }
      } //统计相同个数
      for (let i in newStr) {
        let j = 0;
        res.forEach((item) => {
          if (newStr[i] == item) {
            j++;
          }
        });
        count[i] = newStr[i] + " " + j; //拼接字符串的输出模板，并写入数组中
      }
      let res1 = count.join("、"); //转为字符串
      let res2 = newStr.join("");
      return { res1, res2 }; //多个返回值，以对象的形式返回
    }
    let str3 = "a,a,b,c,c,d";
    console.log(str_unique(str3));
  </script>
</html>
